package com.yihuan.controller;

import com.yihuan.entity.CompanyEntity;
import com.yihuan.entity.SellerEntity;
import com.yihuan.global.IDCardUtil;
import com.yihuan.global.MessageConstant;
import com.yihuan.global.vms_variable_global;
import com.yihuan.service.CompanyService;
import com.yihuan.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:梦醒
 * Date: 2022/4/18 20:56
 * Dream:
 * 信言不美，美言不信。
 * 善者不辩，辩者不善。
 * 知者不博，博者不知。
 * Function:
 */
@RestController
public class SellerController {
    int pageNumber = 0;
    int count = 0;
    //使用map存储数据
    private Map<String, Object> map = new HashMap<>();
    //数据页数和起始值
    private int num;
    //全局访问类
    private vms_variable_global vms_variable = new vms_variable_global();
    @Autowired
    SellerService sellerService;
    @Autowired
    CompanyService companyService;
    
    /**
     * 登录账号
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findSellerBySellerLoginNameAndSellerPassword")
    public Map<String, Object> findSellerBySellerLoginNameAndSellerPassword(@RequestBody SellerEntity model, HttpSession session) {
        List<SellerEntity> sellerEntityList = sellerService.findSellerBySellerLoginNameAndSellerPassword(model);
        //判断账户是否能登录
        if (sellerEntityList.size() == 1) {
            //设置登录昵称
            model.setSellerNickname(sellerEntityList.get(0).getSellerNickname());
            //设置登录账户编号
            model.setSellerID(sellerEntityList.get(0).getSellerID());
            vms_variable.variableName(session, model);
            //如果公司编号为0  则表名未注册公司   否则已注册公司
            if (sellerEntityList.get(0).getCompID() == 0) {
                map.put("code", MessageConstant.HALF_CODE);
                map.put("explain", MessageConstant.SELLER_LOGIN_HALF);
                return map;
            } else {
                CompanyEntity companyEntity = new CompanyEntity();
                companyEntity.setCompID(sellerEntityList.get(0).getCompID());
                //判断公司是否已存在密钥
                List<CompanyEntity> findCompanyByCompIDAndCompCode = companyService.findCompanyByCompIDAndCompCode(companyEntity);
                //存储商铺编号
                session.setAttribute("compID", findCompanyByCompIDAndCompCode.get(0).getCompID());
                //判断公司是否存在禁用情况
                if (findCompanyByCompIDAndCompCode.get(0).getCompCode() == "" || findCompanyByCompIDAndCompCode.get(0).getCompCode() == null) {
                    map.put("code", MessageConstant.HALF_RETURN_CODE);
                    map.put("explain", MessageConstant.SELLER_LOGIN_HALF_RETURNL);
                    return map;
                } else {
                    if (findCompanyByCompIDAndCompCode.get(0).getCompState() == 2) {
                        map.put("code", MessageConstant.RETURN_CODE);
                        map.put("explain", MessageConstant.SELLER_LOGIN__RETURNL);
                        return map;
                    }
                    map.put("code", MessageConstant.SUCCESS_CODE);
                    map.put("explain", MessageConstant.SELLER_LOGIN_SUCCESS);
                    return map;
                }
                
            }
            
        }
        map.put("code", MessageConstant.FALL_CODE);
        map.put("explain", MessageConstant.SELLER_LOGIN_FALL);
        return map;
    }
    
    /**
     * 查询用户账号
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findSellerBySellerLoginName")
    public Map<String, Object> findSellerBySellerLoginName(@RequestBody SellerEntity model, HttpSession session) {
        List<SellerEntity> sellerEntityList = sellerService.findSellerBySellerLoginName(model);
        //判断数据是否存在
        if (sellerEntityList.size() == 1) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.SELLER_SELECT_NAME_FALL);
            return map;
        }
        //账户登录成功
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.SELLER_SELECT_NAME_SUCCESS);
        return map;
    }
    
    /**
     * 登记商户信息
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/insertSellerBySellerData")
    public Map<String, Object> insertSellerBySellerData(@RequestBody SellerEntity model, HttpSession session) {
        //用户出生日期
        model.setSellerDateOfBirth(new IDCardUtil().getBirthday(model.getSellerIDNumber()));
        //用户年龄
        model.setSellerAge(new IDCardUtil().getAge(model.getSellerIDNumber()));
        //用户级别
        model.setJurID(2);
        //用户申请
        model.setSellerState(2);
        //用户申请
        model.setSellerDeleteState(1);
        //设置时间的格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取时间的方法
        Date d = new Date();
        //将获取的时间转换成设置的时间格式进行存储
        model.setSellerDate(sf.format(d));
        //注册数据
        int insertSellerBySellerData = sellerService.insertSellerBySellerData(model);
        //判断注册数据是否存在
        if (insertSellerBySellerData == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.SELLER_REGISTER_FALL);
            return map;
        }
        //账户注册成功
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.SELLER_REGISTER_SUCCESS);
        return map;
    }
    
    /**
     * 更改商户信息
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateSellerBySellerPassword")
    public Map<String, Object> updateSellerBySellerPassword(@RequestBody SellerEntity model, HttpSession session) {
        int updateSellerBySellerPassword = sellerService.updateSellerBySellerPassword(model);
        //判断数据是否存在
        if (updateSellerBySellerPassword == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.UPATE_RESET_PASSWORD_FALL);
            return map;
        }
        //账户登录成功
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.UPATE_RESET_PASSWORD_SUCCESS);
        return map;
    }
    
    /**
     * 分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findSellerBySellerList")
    public Map<String, Object> findSellerBySellerList(@RequestBody SellerEntity model, HttpSession session) {
        List<SellerEntity> findByUserList = sellerService.findSellerBySellerList(model);
        if (findByUserList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.SELLER_LIMIT_FALL);
            return map;
        }
        pageNumber = countDoctorListByID(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("user_List", findByUserList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.SELLER_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 统计分页查询数据
     *
     * @param model
     * @return
     */
    public int countDoctorListByID(SellerEntity model) {
        count = sellerService.countSellerList();
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 模糊分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findSellerBySellerConcatList")
    public Map<String, Object> findSellerBySellerConcatList(@RequestBody SellerEntity model, HttpSession session) {
        List<SellerEntity> findSellerBySellerConcatList = sellerService.findSellerBySellerConcatList(model);
        if (findSellerBySellerConcatList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.SELLER_LIMIT_Concat_FALL);
            return map;
        }
        pageNumber = countSellerConcatList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("user_List", findSellerBySellerConcatList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.SELLER_LIMIT_Concat_SUCCESS);
        return map;
    }
    
    /**
     * 统计模糊分页查询数据
     *
     * @param model
     * @return
     */
    public int countSellerConcatList(SellerEntity model) {
        count = sellerService.countSellerConcatList(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 更改商户信息状态
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateSellerBySellerState")
    public Map<String, Object> updateSellerBySellerState(@RequestBody SellerEntity model, HttpSession session) {
        int updateSellerBySellerState = sellerService.updateSellerBySellerState(model);
        //判断数据是否存在
        if (updateSellerBySellerState == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.SELLER_STATE_FALL);
            return map;
        }
        //账户登录成功
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.SELLER_STATE_SUCCESS);
        return map;
    }
    
    /**
     * 个人信息
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findSellerBySellerID")
    public Map<String, Object> findSellerBySellerID(@RequestBody SellerEntity model, HttpSession session) {
        model.setSellerID((Integer) session.getAttribute("SellerID"));
        List<SellerEntity> findSellerBySellerID = sellerService.findSellerBySellerID(model);
        if (findSellerBySellerID.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.SELLER_LIMIT_Concat_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("seller_List", findSellerBySellerID);
        map.put("explain ", MessageConstant.SELLER_LIMIT_Concat_SUCCESS);
        return map;
    }
}
